package com.hbx.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbx.commonutils.JwtUtils;
import com.hbx.commonutils.R;
import com.hbx.commonutils.ordervo.CourseWebVoOrder;
import com.hbx.eduservice.client.OrderClient;
import com.hbx.eduservice.entity.EduCourse;
import com.hbx.eduservice.entity.EduTeacher;
import com.hbx.eduservice.entity.chapter.ChapterVo;
import com.hbx.eduservice.entity.frontvo.CourseFrontVo;
import com.hbx.eduservice.entity.frontvo.CourseWebVo;
import com.hbx.eduservice.service.EduChapterService;
import com.hbx.eduservice.service.EduCourseService;
import com.hbx.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author hbx
 * @Date 2023/10/17 16:13
 */
@Api(tags = {"课程前台接口管理"})
@RestController
@CrossOrigin
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    /**
     * 条件分页查询课程
     * @param current
     * @param limit
     * @param courseFrontVo
     * @return
     */
    @PostMapping("/getFrontCourseList/{current}/{limit}")
    public R getFrontCourseList(@PathVariable Long current, @PathVariable Long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> page = new Page<>(current,limit);
        Map<String,Object> map = courseService.getCourseFrontList(page,courseFrontVo);
        return R.ok().data(map);
    }

    /**
     * 课程详情的方法
     * @param courseId
     * @return
     */
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        //根据课程id和用户id查询订单是否已支付
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = false;
        if(memberIdByJwtToken != null){
            buyCourse = orderClient.isBuyCourse(courseId, memberIdByJwtToken);
        }
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("isBuy",buyCourse);
    }

    //根据课程id返回课程信息，通过远程调用，用于创建订单
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder order = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,order);
        return order;
    }
}
